import MySQLdb
from DatabaseConnector import connection
import CDALIndexer
from time import strftime
import sys
import operator

class dbtester:
        
    def __new__(cls, *args, **kargs):
        if cls.instance is None:
            cls.instance = object.__new__(cls, *args, **kargs)
        return cls.instance
          
    def testbody(self, cursor):
        for i in range(2, 10):
            try:
                print i
                cursor.execute("insert into hosrep.resulttext_table values(+" + str(i) +",'',1,'','','2008-04-18 20:10:23')")
                            
            except MySQLdb.OperationalError, message:
                errorMessage = "Error %d:\n%s" % (message[ 0 ], message[ 1 ] )
                print errorMessage
    
                
if __name__ == "__main__":
    time = strftime("%Y-%m-%d %H:%M:%S")
    print time
#    print idx.EncodeRequestID(1)
     
#    if len(sys.argv) == 1:
#        sys.exit("Arguments missed")
##    import Translator
##    import TokenMatcher
##    ts = Translator.Translator()
#    interval = 2
#    conc_list = {}
#    indexer = CDALIndexer.CDALIndexer(sys.argv[1], sys.argv[2])
#    print indexer.getCurrentTime()
#    cursor = connection.cursor()
#    cursor.execute("select r1.`RequestID`, r1.`ResultText`, r2.`DateTime_Request` FROM hosrep.resulttext_table r1, hosrep.request_table r2 where r1.requestid = r2.requestid and r1.`RequestID` >= " + str(sys.argv[1]) + " and r1.`RequestID` < " + str(sys.argv[2]) + " and r1.`Flag` = 0")
#    for i, tuple in enumerate(cursor):
#        #    reqid = str(reqid).strip('L')
#            if i% interval ==0:
#                print "round no.:\t" + str(i/interval)
#                req_start = tuple[0]
#            reqcode = indexer.EncodeRequestID(tuple[0])
#            reqid = tuple[0]
#            text = tuple[1]
#            datetime = str(tuple[2])
#            
#            for ((s, e), ids) in TokenMatcher.match_sentence(text.split()):
#                conc_list = ts.translator(ids, conc_list)
#            indexer.WriteToIndex(reqcode, conc_list, datetime)
#            
##            print indexer.info_matrix
##            print indexer.getCurrentTime()
##            print indexer.vs
##            print indexer.seg_start
##            print indexer.seg_end
#            
#            if (i+1)% interval == 0:
#                req_end = reqid
#                indexer.BatchSQL(req_start, req_end)
#
#



#    def EncodeRequestID(self, reqid):
#    rscode = indexer.EncodeRequestID(testcode)
#    print rscode
##    def DecodeRequestID(self, reqcode):
#    rsint = indexer.DecodeRequestID(rscode)
#    print rsint
##    def InsertReqSeg(self, reqcode, segment):
#    indexer.info_matrix[0] = {"test":{"9":"001,110", "10":"123"}}
#    indexer.InsertReqSeg(rscode, indexer.info_matrix[0]["test"])
#    print indexer.info_matrix
#    def DecomposeRequest(self, req_string):
#    def ComposeRequest(self, req_ls):      
#    def InsertReq(self, req_ls):
#    def WriteToIndex(self, reqcode, concept_list, datetime):
#    def BatchSQL(self, req_start, req_end):
   
        
